use async_trait::async_trait;
use my_rust_core::security::{
    id_field::IdField,
    request_map_manager::{RequestMap, RequestMapQuery},
};
use sea_orm::{DatabaseConnection, EntityTrait};
use std::sync::Arc;

use crate::entities;

pub struct RequestMapQueryImpl {
    pub db: Arc<DatabaseConnection>,
}

impl RequestMapQueryImpl {
    pub fn new(db: Arc<DatabaseConnection>) -> Self {
        RequestMapQueryImpl { db }
    }
}

#[async_trait]
impl RequestMapQuery for RequestMapQueryImpl {
    async fn query_all(&self) -> Vec<RequestMap> {
        entities::prelude::RequestMap::find()
            .all(self.db.as_ref())
            .await
            .unwrap()
            .iter()
            .map(|p| RequestMap {
                id: IdField::Text(p.id.clone()),
                config_attribute: p.config_attribute.clone(),
                http_method: p.http_method.clone(),
                url: p.url.clone(),
            })
            .collect()
    }
}
